package com.resource.exammanage.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.resource.common.utils.PageUtils;
import com.resource.common.utils.R;
import com.resource.exammanage.config.TimeJudgeUtils;
import com.resource.exammanage.entity.TExamInformationEntity;
import com.resource.exammanage.service.TExamInformationDetailService;
import com.resource.exammanage.service.TExamInformationService;
import com.resource.studentmanage.feign.StudentExamRemote;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;


/**
 * 考试信息表
 *
 * @author zkx
 * @email 1486664603@qq.com
 * @date 2023-03-06 20:53:55
 */
@RestController
@RequestMapping("exam/texaminformation")
public class TExamInformationController {
    @Autowired
    private TExamInformationService tExamInformationService;

    @Autowired
    private TExamInformationDetailService tExamInformationDetailService;

    @Autowired
    private StudentExamRemote studenExamRemote;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
     PageUtils page = tExamInformationService.queryPage(params);
       return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        TExamInformationEntity tExamInformation = tExamInformationService.getById(id);

        return R.ok().put("tExamInformation", tExamInformation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TExamInformationEntity tExamInformation) {
        tExamInformationService.save(tExamInformation);
        //保存考试的信息
        tExamInformationDetailService.saveRelateDetail(tExamInformation);
        HashMap<String, String> stringHashMap = new HashMap<>();
        stringHashMap.put("examCode",tExamInformation.getExamCode());
        stringHashMap.put("examName",tExamInformation.getExamName());
        stringHashMap.put("examTeacherName",tExamInformation.getExamTeacherName());
        stringHashMap.put("examStudentCode",tExamInformation.getExamStudentCode());
        stringHashMap.put("examStudentName",tExamInformation.getExamStudentName());
        stringHashMap.put("examProgress","未完成");
        //生成学生考试记录
        studenExamRemote.dealStudentRecord(stringHashMap);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TExamInformationEntity tExamInformation) {
        tExamInformationService.updateById(tExamInformation);
        tExamInformationDetailService.saveRelateDetail(tExamInformation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        tExamInformationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    //开始考试
    @RequestMapping("/startExam/{id}")
    public R startExam(@PathVariable("id") Long id) throws ParseException {
        TExamInformationEntity tExamInformation = tExamInformationService.getById(id);
        String examTime = tExamInformation.getExamTime();
        if(StringUtils.isNotEmpty(examTime)){
            Date nowdate = new Date();
            //考试入场最后时间
            Date logindate = TimeJudgeUtils.addMinuteTime(examTime, 10);
            Date startdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(examTime);
            if(nowdate.before(startdate)){
                return R.error("考试未开始,请稍后再试");
            }else if(nowdate.after(logindate)){
                return R.error("已过考试时间,不能进行考试");
            }
            return R.ok();
        }else{
            return R.error("考试时间不能为空");
        }
    }

}
